package com.xasz.text2vec.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xasz.text2vec.dao.entity.KnowledgeBase;
import com.xasz.text2vec.dto.KnowledgeDTO;
import com.xasz.text2vec.service.KnowledgeBaseService;
import com.xasz.text2vec.utils.ToPo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cai
 * @since 2023-06-29
 */
@RestController
@RequestMapping("/knowledge")
public class KnowledgeController {
    @Autowired
    KnowledgeBaseService knowledgeBaseService;
    /**
     * 新增单条知识
     *
     */
    @PutMapping("/addKnowledge")
    public ResponseEntity<Boolean> addKnowledge(@RequestBody KnowledgeDTO knowledgeDTO){
        return ResponseEntity.status(HttpStatus.CREATED).body(knowledgeBaseService.save(ToPo.KnowledgeToPo(knowledgeDTO)));
    }

    /**
     * 删除单条知识
     *
     */
    @DeleteMapping("/deleteKnowledge")
    public ResponseEntity<Boolean> deleteKnowledge(@RequestParam(value = "id") int id){
        return ResponseEntity.status(HttpStatus.CREATED).body(knowledgeBaseService.removeById(id));
    }
    /**
     * 查询sysCode的知识列表,当传入为admin时查询全部
     */
    @GetMapping("/getKnowledge")
    public Page<KnowledgeBase> getKnowledgeList(@RequestParam(value = "sysCode") String sysCode,
                                                @RequestParam(value = "pageNumber") int pageNumber,
                                                @RequestParam(value = "pageSize") int pageSize){
        Page<KnowledgeBase> page = Page.of(pageNumber,pageSize);
        if (Objects.equals(sysCode, "admin")){
            return knowledgeBaseService.page(page);
        }
        Wrapper<KnowledgeBase> queryWrapper=new QueryWrapper<KnowledgeBase>().eq("sys_code",sysCode);
        return knowledgeBaseService.page(page,queryWrapper);
    }
    /**
     * 修改单条知识
     */
    @PostMapping("/updateKnowledge")
    public ResponseEntity<Boolean> updateKnowledge(@RequestBody KnowledgeDTO knowledgeDTO){
        return ResponseEntity.status(HttpStatus.CREATED).body(knowledgeBaseService.updateById(ToPo.KnowledgeToPo(knowledgeDTO)));
    }

    /**
     * 获取单条知识
     */
    @GetMapping("/getKnowledgeById")
    public ResponseEntity<KnowledgeBase> getKnowledgeById(@RequestParam(value = "id") int id,
                                                          @RequestParam(value = "qContent") String qContent){
        KnowledgeBase byId = knowledgeBaseService.selectById(id,qContent);
        return ResponseEntity.status(HttpStatus.CREATED).body(byId);
    }
}

